package demo;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-03-31
 * Time: 21:18
 */
public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("1.95.194.117");//IP
        connectionFactory.setPort(5672);//端口号
        connectionFactory.setUsername("wangkun");//用户名
        connectionFactory.setPassword("200411");//密码
        connectionFactory.setVirtualHost("study");//虚拟机

        //2.开启通信
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        //3.声明交换机 (此处使用内置默认的交换机)


        //4.声明队列
        /**
         *queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
         *                                  Map<String, Object> arguments)
         *
         * queue:队列的名称
         * durable:可持久化
         * exclusive:是否独占
         * autoDelete: 是否自动删除
         * arguments: 参数
         */

        channel.queueDeclare("hello",true,false,false,null);

        //5.发送消息
        /**
         *basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body)
         *
         * exchange: 交换机
         * routingKey: 内置交换机，routingkey和队列名称保持一致
         * props: 属性配置
         * body: 消息
         */

        for (int i = 0; i < 10; i++) {
            String message = "hello RabbitMQ + i" + i;
            channel.basicPublish("","hello",null,message.getBytes());
        }

        /*String message = "hello RabbitMQ";
        channel.basicPublish("","hello",null,message.getBytes());*/

        //6.资源释放
        channel.close();
        connection.close();

    }
}
